using System;
using System.Collections;
using System.Windows.Forms;
using System.Drawing;

namespace SecureCommander.Helper
{
	/// <summary>
	/// Maintains a list of currently added file extensions
	/// </summary>
	public class IconListManager
	{
        private IconReader.IconSize _iconSize;
        private Hashtable _images = new Hashtable();	

        public IconListManager(IconReader.IconSize iconSize)
        {

            _images.Clear();
            _iconSize = iconSize;
        }

        public Bitmap GetFileImage(string filePath)
		{
			// Check if the file exists, otherwise, throw exception.
			if (!System.IO.File.Exists( filePath )) throw new System.IO.FileNotFoundException("File does not exist");
			
			// Split it down so we can get the extension
			string[] splitPath = filePath.Split(new Char[] {'.'});
			string extension = ((string)splitPath.GetValue( splitPath.GetUpperBound(0) )).ToUpper();
            //Check that we haven't already got the extension, if we have, then
            //return back its index
            if (!_images.ContainsKey(extension))
            {
                // It's not already been added, so add it and record its position.
                _images.Add(extension, IconReader.GetFileIcon(filePath, _iconSize, false).ToBitmap());
            }

            return (Bitmap)_images[extension];
		}


        public Bitmap GetFolderIcon(string dirPath)
        {
            // Check if the file exists, otherwise, throw exception.
            if (!System.IO.Directory.Exists(dirPath)) throw new System.IO.FileNotFoundException("Folder does not exist");

            // Split it down so we can get the extension           
            string extension = "DIR";
            if (!_images.ContainsKey(extension))
            {
                // It's not already been added, so add it and record its position.
                _images.Add(extension, IconReader.GetFolderIcon(dirPath, _iconSize, IconReader.FolderType.Open).ToBitmap());
            }
            //return (Bitmap)_images[extension]; 
            return IconReader.GetFolderIcon(dirPath, _iconSize, IconReader.FolderType.Open).ToBitmap();
        }
	}
}
